package com.example.coursehub.controller;

import com.example.coursehub.entity.Chapter;
import com.example.coursehub.service.ChapterService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/chapters")
public class ChapterController {

    private final ChapterService chapterService;

    public ChapterController(ChapterService chapterService) {
        this.chapterService = chapterService;
    }

    /**
     * 创建新章节
     * @param chapter 章节对象
     * @return 创建的章节及HTTP状态码201
     */
    @PostMapping
    public ResponseEntity<Chapter> createChapter(@RequestBody Chapter chapter) {
        return new ResponseEntity<>(chapterService.createChapter(chapter), HttpStatus.CREATED);
    }

    /**
     * 更新章节信息
     * @param chapterId 章节ID
     * @param chapter 章节对象
     * @return 更新后的章节及HTTP状态码200
     */
    @PutMapping("/{chapterId}")
    public ResponseEntity<Chapter> updateChapter(@PathVariable String chapterId, @RequestBody Chapter chapter) {
        return ResponseEntity.ok(chapterService.updateChapter(chapterId, chapter));
    }

    /**
     * 根据ID删除章节
     * @param chapterId 章节ID
     * @return HTTP状态码204
     */
    @DeleteMapping("/{chapterId}")
    public ResponseEntity<Void> deleteChapter(@PathVariable String chapterId) {
        chapterService.deleteChapter(chapterId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 根据ID获取章节
     * @param chapterId 章节ID
     * @return 章节对象及HTTP状态码200
     */
    @GetMapping("/{chapterId}")
    public ResponseEntity<Chapter> getChapterById(@PathVariable String chapterId) {
        return ResponseEntity.ok(chapterService.getChapterById(chapterId));
    }

    /**
     * 获取指定课程下的所有章节
     * @param courseId 课程ID
     * @return 章节列表及HTTP状态码200
     */
    @GetMapping("/course/{courseId}")
    public ResponseEntity<List<Chapter>> getChaptersByCourseId(@PathVariable String courseId) {
        return ResponseEntity.ok(chapterService.getChaptersByCourseId(courseId));
    }

    /**
     * 获取所有章节
     * @return 章节列表及HTTP状态码200
     */
    @GetMapping
    public ResponseEntity<List<Chapter>> getAllChapters() {
        return ResponseEntity.ok(chapterService.getAllChapters());
    }

    /**
     * 获取学生已学习的章节
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 已学习的章节列表及HTTP状态码200
     */
    @GetMapping("/student/{userId}/course/{courseId}/learned")
    public ResponseEntity<List<Chapter>> getLearnedChaptersByStudent(
            @PathVariable String userId,
            @PathVariable String courseId) {
        return ResponseEntity.ok(chapterService.getLearnedChaptersByStudent(userId, courseId));
    }

    /**
     * 标记章节为已完成状态
     * @param userId 用户ID
     * @param chapterId 章节ID
     * @return HTTP状态码204
     */
    @PostMapping("/student/{userId}/{chapterId}/finish")
    public ResponseEntity<Void>markChapterAsFinished(
            @PathVariable String userId,
            @PathVariable String chapterId) {
        chapterService.markChapterAsFinished(userId, chapterId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}